package com.linxz.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * @author Linxz
 * 创建日期：2019年07月10日 10:59
 * version：v4.0.1
 * 描述：
 */
public class NumberUtils {
    /**
     *
     * double 数据保留小数操作
     * */
    public static String scaleNumber(double targetNumber, int scale ){
        NumberFormat ddf1 = NumberFormat.getNumberInstance();
        ddf1.setMaximumFractionDigits(scale);
        return ddf1.format(targetNumber);
    }

    public static String scaleNumber(String targetNumber, int scale ){
        double number=Double.parseDouble(targetNumber);
        NumberFormat ddf1 = NumberFormat.getNumberInstance();
        ddf1.setMaximumFractionDigits(scale);
        return ddf1.format(number);
    }

    public static String scaleNumber(String targetNumber){
        DecimalFormat df = new DecimalFormat("######0.00");
        return df.format(Double.parseDouble(targetNumber));
    }



    /**进行减法运算*/
    public static double subtract(double v1,double v2){
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2).doubleValue();
    }

    /**进行减法运算*/
    public static double subtract(BigDecimal b1,BigDecimal b2){
        return b1.subtract(b2).doubleValue();
    }

    /**进行减法运算*/
    public static String subtractSacle(BigDecimal b1,BigDecimal b2){
        return scaleNumber(b1.subtract(b2).doubleValue()+"");
    }

    public static double subtract(String v1,String v2){
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.subtract(b2).doubleValue();
    }

    /**乘法*/
    public static double multiply(double v1,double v2){
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2).doubleValue();
    }


    public static String formatNumberofDistance(String distanceStr){
        double distance=Double.parseDouble(distanceStr);
        if(distance<1000){
            return scaleNumber(distance,1)+"M";
        }else{
            return scaleNumber(distance/1000,2)+"KM";
        }
    }



    /**
     * bty转M
     * */
    public static String parseSizeOfByteToM(int size){
        double sizeDouble=Double.parseDouble(size+"");
        double sizeofM=sizeDouble/1024/1024;
        return scaleNumber(sizeofM+"")+"M";
    }


    /**
     * 转换百分比
     * */
    public static String toPercent(int progress,int total){
        double percent=Double.parseDouble(progress+"")/Double.parseDouble(total+"");
        return scaleNumber(percent+"")+"%";
    }
}
